﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlDoc = HtmlAgilityPack.HtmlDocument;
using WebFunctions;
using Newtonsoft.Json;
using System.IO;
using System.Windows.Forms;

namespace AutoIndustryFiles
{
    class faOmronScraper : FileLinksScraper2
    {
        public faOmronScraper()
        {
            this.MainPage = "http://www.fa.omron.com.cn/document";
            this.CompanyName = "欧姆龙工业自动化";
            this.CompanyUrl = "http://www.fa.omron.com.cn/document";
            this.ColumnName = "首页 -> 资料汇总";
            //this.ReviseXPath += new OnReviseXPath(FuJiScraper_ReviseXPath);            
        }

        public void GetDownloadIndex()
        {
            string[] categories = File.ReadAllLines(@"D:\外包项目\自动化资料下载\Output\fa.omron\分类.txt");

            var files = from file in Directory.EnumerateFiles(@"D:\外包项目\自动化资料下载\Output\fa.omron\Dump-0512-10-43-42\", "*.html", SearchOption.AllDirectories)
                        orderby file ascending
                        select new
                        {
                            File = file
                        }
;
            if (files.Count() == 0)
            {
                return;
            }
            StringBuilder sb = new StringBuilder();
            foreach (var f in files)
            {
                HtmlDoc doc = new HtmlDoc();
                doc.LoadHtml(File.ReadAllText(f.File,Encoding.GetEncoding("UTF-8")));

                string categoryid = Path.GetFileNameWithoutExtension(f.File);
                string categoryname = "";
                for (int i = 0; i <= categories.GetUpperBound(0); i++)
                {
                    string[] ps = categories[i].Split(new string[] { "\t" },StringSplitOptions.RemoveEmptyEntries);
                    if (ps[1] == categoryid)
                        categoryname = ps[0];
                }

                HtmlAgilityPack.HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(@"//table[@class=""dTab""]/tr[position() > 1]");
                if (hnc == null)
                    continue;
                
                foreach (HtmlAgilityPack.HtmlNode node in hnc)
                {
                    string fileName = node.SelectSingleNode("td[1]").InnerText.Trim();
                    HtmlAgilityPack.HtmlNodeCollection hnc2 = node.SelectNodes("td[a]");
                    if (hnc2 == null)
                        continue;
                    string urls = "";
                    foreach (HtmlAgilityPack.HtmlNode node2 in hnc2)
                    {
                        urls = urls + ";" + FormatUrl(node2.SelectSingleNode("a").GetAttributeValue("href", ""),"http://www.fa.omron.com.cn/");
                    }

                    sb.Append(categoryname + ";" + fileName + urls + Environment.NewLine);
                }
            }

            File.WriteAllText(@"D:\外包项目\自动化资料下载\Output\fa.txt", sb.ToString(),Encoding.GetEncoding("UTF-8"));
        }

        public override void GenerateAllLinks(string param)
        {
            string[] all_products = File.ReadAllLines(@"D:\外包项目\自动化资料下载\Output\fa.txt");

            //每一行代表一个产品
            //栏目;产品名称;文档链接1;文档链接2...
            //样本包含关键字"catalog.html",手册包含关键字"manual.html",帮助包含"faq/"，手册与说明书同一网页

            foreach (string s in all_products)
            {
                string[] product = s.Split(new string[] { ";" },StringSplitOptions.RemoveEmptyEntries);
                string category = product[0];
                string name = product[1];
                string href = "";
                int cnt = product.GetUpperBound(0);

                List<string> urls = new List<string> { };

                for (int i = 2; i <= cnt; i++)
                    if (!urls.Contains(product[i]) && !product[i].Contains("faq/"))
                        urls.Add(product[i]);

                bool f = false;
                foreach (string url in urls)
                {
                    HtmlDoc doc = new HtmlDoc();
                    doc.LoadHtml(WebFuncs.OpenUrlEx(url, "UTF-8"));
                    UpdateMsg("faOmronScraper", url, ref f);

                    HtmlAgilityPack.HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(@"//table[@class=""tableA01""]/tbody/tr");

                    if (hnc == null)
                        continue;
                    foreach (HtmlAgilityPack.HtmlNode node in hnc)
                    {
                        name = node.SelectSingleNode("td[1]").InnerText.Trim();
                        href = node.SelectSingleNode("td[3]/span/a").GetAttributeValue("href", "");

                        FileLink lnk_new = new FileLink();
                        lnk_new.Id = FileLinks.Count.ToString();
                        lnk_new.Name = name;
                        if (lnk_new.Name.LastIndexOf(".") > 0)
                            lnk_new.FileType = url.IndexOf("catalog.html") > 0 ? "样本" : "手册说明书";
                        lnk_new.ReleaseDate = "";
                        lnk_new.Url = href;
                        lnk_new.Url = FormatUrl(lnk_new.Url, "http://www.fa.omron.com.cn/");
                        lnk_new.IsFileLink = true;
                        lnk_new.CategoryPath = category;
                        FileLinks.Add(lnk_new);
                    }
                }
            }
        }
    }
}
